package org.test.client.ui.component.panel;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.test.client.ui.component.window.PopupWindow;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;
import org.test.client.ui.util.run.ExpandCompRun;
import org.test.client.ui.util.run.RunCallBack;
import org.test.debug.Debug;

/**
 * 在 flowlayout 或者 layout 为null时 伸缩与关闭 表现正常
 * 其他 布局管理器 还未 兼容
 * @author Guodeqi
 *
 */
public class TitlePanePanel extends FadePanel {
	private String closeIcon = "fa-times";
	private String expandedIcon = "fa-chevron-down";
	private String collapsedIcon = "fa-chevron-up";
	private String configIcon = "fa-cog";
	private Color operIconColor = Icons.getColor("555555");
	private int operIconw = 16;
	private int tagw = 26;
	private int tagh = operIconw+4;
	private int tagDiameter = 20;
	private Color tagBorderColor = Icons.getColor("e6e6e6");
	
	private int diameter = 6;
	private int titleHeight = 38;
	private Color titleBackColor1 = Icons.getColor("fcfcfc");
	private Color titleBackColor2 = Icons.getColor("f3f3f3");
	private Color titleBackColor3 = Icons.getColor("e7e7e7");
	private Color borderColor = Icons.getColor("dedede");

	
	private Font titleFont = new Font(Font.DIALOG, Font.BOLD, 13);
	private Color titleFontColor = Icons.getColor("317eac");
	private int titleIconw = 20;
	
	private int titleMarginLeft = 20;
	private int titleMarginRight= 20;
	private int elementSpace= 5;
	
	
	////////////以上是 paintComponent 用到的////////////////////////////////
	private TitlePanePanel thisPanel = this;
	private boolean useConfig = true;

	private String title = null;
	private String titleIcon = null;
	
	private Rectangle rectClose = null;
	private Rectangle rectColl = null;
	private Rectangle rectConfig = null;
	//面板的 宽度 与高度
	private int nowWidth = 0;
	private int nowHeigth = 0;
	private int normalHeight = 0;
	
	private BufferedImage biTitle = null;
	private BufferedImage biCfg = null;
	private BufferedImage biColl = null;
	private BufferedImage biExp = null;
	private BufferedImage biClose= null;
	MouseListenerForPanelIcon iconListener = null;
	//是否展开  当前面板展开 与 否
	private boolean expanded = true;
	
	public boolean startDrag = false;
	private boolean isDoingExpand = false;
	
	public TitlePanePanel(String title,String titleIcon){
		this.title = title;
		this. titleIcon = titleIcon;
	}
	
	public TitlePanePanel doInit(){
		if(titleIcon == null){
//			titleIcon = "fa-plus";
			//若没有 标题icon 则 创建一个空的
			biTitle = new BufferedImage(1, titleIconw, BufferedImage.TRANSLUCENT);
		}else{
			biTitle = Icons.getImgByDesc(titleIcon, titleIconw, titleFontColor);
		}
		biCfg = Icons.getImgByDesc(configIcon, operIconw, operIconColor);
		biColl = Icons.getImgByDesc(collapsedIcon, operIconw, operIconColor);
		biExp = Icons.getImgByDesc(expandedIcon, operIconw, operIconColor);
		biClose = Icons.getImgByDesc(closeIcon, operIconw, operIconColor);

		iconListener = new MouseListenerForPanelIcon(this);
		this.addMouseListener(iconListener);
		this.addMouseMotionListener(iconListener);
		return this;
	}
	
	@Override
	public Insets getInsets() {
		return new Insets(titleHeight, 1, 1, 1);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d  = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		Paint oldPaint = g2d.getPaint();
		nowWidth = this.getWidth();
		nowHeigth = this.getHeight();
		if( normalHeight == 0){
//			Container parent2 = this.getParent();
//			if(parent2.getLayout()!=null){
//				
//			}
			normalHeight = nowHeigth;
		}
		
		GradientPaint paint1 = new GradientPaint(0, 0, titleBackColor1, 0, titleHeight/2, titleBackColor2);
		GradientPaint paint2 = new GradientPaint(0, titleHeight/2, titleBackColor2, 0, titleHeight, titleBackColor3);
		BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
		g2d.setPaint(paint1);
		g2d.fillRoundRect(0, 0, nowWidth, titleHeight/2, diameter, diameter);
		g2d.setPaint(paint2);
		g2d.fillRoundRect(0, titleHeight/2, nowWidth, titleHeight/2, diameter, diameter);
		
		g2d.setStroke(bs1);
		g2d.setColor(borderColor);
		g2d.drawRoundRect(0, 0, nowWidth-1, nowHeigth-1, diameter, diameter);
		g2d.drawLine(0, titleHeight, nowWidth, titleHeight);
		
		int x,y;
		x = titleMarginLeft ;
		y = titleHeight/2 - biTitle.getHeight()/2;
		g2d.drawImage(biTitle, x, y, biTitle.getWidth(), biTitle.getHeight(), null);
		
		g2d.setFont(titleFont);
		g2d.setColor(titleFontColor);
		x = x +biTitle.getWidth() +elementSpace;
		FontMetrics fm = g2d.getFontMetrics();
		//与图片 底部对齐 减一是因为 加载fontAware 图标的时候 会有1px 的margin
		y =  y + biTitle.getHeight() - UIUtil.getTxty(g2d) -1 ;
		g2d.drawString(title, x, y);
		
		x = nowWidth - titleMarginRight - tagw;
		//与图片 底部对齐
		y = titleHeight/2 + biTitle.getHeight()/2 - tagh -1;
		rectClose = drawTag(g2d,x,y,biClose);
		iconListener.setRectClose(rectClose);
		
		x = x -  elementSpace -tagw ;
		if(expanded){
			rectColl = drawTag(g2d,x,y,biColl);
		}else{
			rectColl = drawTag(g2d,x,y,biExp);
		}
		iconListener.setRectColl(rectColl);
		
		if(useConfig){
			x = x -  elementSpace -tagw ;
			rectConfig = drawTag(g2d,x,y,biCfg);
			iconListener.setRectConfig(rectConfig);
		}
//		super.paintComponent(g2d);
	}
	
	
	
	public Rectangle drawTag(Graphics2D g2d , int x, int y, BufferedImage img){
		
		g2d.setColor(tagBorderColor);
		g2d.drawRoundRect(x, y, tagw, tagh,tagDiameter, tagDiameter);
		g2d.setColor(Color.white);
		g2d.fillRoundRect(x+1, y+1, tagw-1, tagh-1,tagDiameter, tagDiameter);
		
		int imgw = img.getWidth();
		int imgh = img.getHeight();
		x = x + (tagw - imgw)/2;
		y = y + (tagh - imgh)/2;
		g2d.drawImage(img, x, y, imgw,imgh , null);
		
		return new Rectangle(x, y, tagw, tagh);
	}
	
	public void doConfig(MouseEvent me){
		Debug.debug("this is doConfig");
	}
	
	public void doClose(MouseEvent me){
		Debug.debug("this is do close");
		fadeOut(new RunCallBack() {
			public Object doAfterRun(Object obj) {
				Container pc = thisPanel.getParent();
				pc.remove(thisPanel);
				pc.doLayout();
				pc.repaint();
//				pc.getParent().repaint();
				return null;
			}
		});
	}
	
	public void doExpand(MouseEvent me){
		if(isDoingExpand){
			PopupWindow instance = PopupWindow.getInstance("面板正在伸缩,请稍后.", PopupWindow.TYPE_WARNING);
			instance.setVisible(true);
			return ;
		}
		Container p = this.getParent();
		LayoutManager layout = p.getLayout();
		boolean flag = layout instanceof FlowLayout;
//		if(layout!=null && !flag){
//			return ;
//		}

		isDoingExpand = true;
		int h = 0;
		if(expanded){
			expanded = false;
			 h = nowHeigth - titleHeight;
		}else{
			expanded = true;
			h = normalHeight - titleHeight;
		}
		ExpandCompRun run = new ExpandCompRun(thisPanel, expanded, h,new RunCallBack() {
			public Object doAfterRun(Object obj) {
				isDoingExpand = false;
				thisPanel.repaint();
				return null;
			}
		});
		Thread thread = new Thread(run);
		thread.start();
		
	}
	
	/***test**/
	public static void main(String[] args) {
		JPanel pppp = new JPanel();
		
		TitlePanePanel ttp = new TitlePanePanel("这是一个测试1", "fa-th").doInit();
		ttp.add(new JButton("AAAAAAA1"));
		ttp.add(new JLabel("AAAAAAA1"));
		pppp.add(ttp);
		ttp.setBounds(50, 10, 300, 200);
			
		TitlePanePanel ttp2 = new TitlePanePanel("这是一个测试2", "fa-th").doInit();
		ttp2.add(new JButton("AAAAAAA2"));
		pppp.add(ttp2);
		ttp2.setBounds(370, 10, 300, 200);
			
		TitlePanePanel ttp3 = new TitlePanePanel("这是一个测试3", "fa-th").doInit();
		ttp3.add(new JButton("AAAAAAA3"));
		pppp.add(ttp3);
		ttp3.setBounds(50, 220, 300, 200);
		
		ttp.setPreferredSize(new Dimension(301,200));
		ttp2.setPreferredSize(new Dimension(301,200));
		ttp3.setPreferredSize(new Dimension(301,200));
//		pppp.setLayout(null);
		
		
		JFrame fff = new JFrame();
		Container contentPane = fff.getContentPane();
		contentPane.add(pppp);
		fff.setDefaultCloseOperation(fff.EXIT_ON_CLOSE);
		fff.setSize(700, 500);
		fff.setLocationRelativeTo(null);
		fff.setVisible(true);
	}
	
	/**Get and Set****************************************************/

	public String getCloseIcon() {
		return closeIcon;
	}

	public void setCloseIcon(String closeIcon) {
		this.closeIcon = closeIcon;
	}

	public String getExpandedIcon() {
		return expandedIcon;
	}

	public void setExpandedIcon(String expandedIcon) {
		this.expandedIcon = expandedIcon;
	}

	public String getCollapsedIcon() {
		return collapsedIcon;
	}

	public void setCollapsedIcon(String collapsedIcon) {
		this.collapsedIcon = collapsedIcon;
	}

	public String getConfigIcon() {
		return configIcon;
	}

	public void setConfigIcon(String configIcon) {
		this.configIcon = configIcon;
	}

	public Color getOperIconColor() {
		return operIconColor;
	}

	public void setOperIconColor(Color operIconColor) {
		this.operIconColor = operIconColor;
	}

	public int getOperIconw() {
		return operIconw;
	}

	public void setOperIconw(int operIconw) {
		this.operIconw = operIconw;
	}

	public int getTagw() {
		return tagw;
	}

	public void setTagw(int tagw) {
		this.tagw = tagw;
	}

	public int getTagh() {
		return tagh;
	}

	public void setTagh(int tagh) {
		this.tagh = tagh;
	}

	public int getTagDiameter() {
		return tagDiameter;
	}

	public void setTagDiameter(int tagDiameter) {
		this.tagDiameter = tagDiameter;
	}

	public Color getTagBorderColor() {
		return tagBorderColor;
	}

	public void setTagBorderColor(Color tagBorderColor) {
		this.tagBorderColor = tagBorderColor;
	}

	public int getDiameter() {
		return diameter;
	}

	public void setDiameter(int diameter) {
		this.diameter = diameter;
	}

	public int getTitleHeight() {
		return titleHeight;
	}

	public void setTitleHeight(int titleHeight) {
		this.titleHeight = titleHeight;
	}

	public Color getTitleBackColor1() {
		return titleBackColor1;
	}

	public void setTitleBackColor1(Color titleBackColor1) {
		this.titleBackColor1 = titleBackColor1;
	}

	public Color getTitleBackColor2() {
		return titleBackColor2;
	}

	public void setTitleBackColor2(Color titleBackColor2) {
		this.titleBackColor2 = titleBackColor2;
	}

	public Color getTitleBackColor3() {
		return titleBackColor3;
	}

	public void setTitleBackColor3(Color titleBackColor3) {
		this.titleBackColor3 = titleBackColor3;
	}

	public Color getBorderColor() {
		return borderColor;
	}

	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
	}

	public Font getTitleFont() {
		return titleFont;
	}

	public void setTitleFont(Font titleFont) {
		this.titleFont = titleFont;
	}

	public Color getTitleFontColor() {
		return titleFontColor;
	}

	public void setTitleFontColor(Color titleFontColor) {
		this.titleFontColor = titleFontColor;
	}

	public int getTitleIconw() {
		return titleIconw;
	}

	public void setTitleIconw(int titleIconw) {
		this.titleIconw = titleIconw;
	}

	public int getTitleMarginLeft() {
		return titleMarginLeft;
	}

	public void setTitleMarginLeft(int titleMarginLeft) {
		this.titleMarginLeft = titleMarginLeft;
	}

	public int getTitleMarginRight() {
		return titleMarginRight;
	}

	public void setTitleMarginRight(int titleMarginRight) {
		this.titleMarginRight = titleMarginRight;
	}

	public int getElementSpace() {
		return elementSpace;
	}

	public void setElementSpace(int elementSpace) {
		this.elementSpace = elementSpace;
	}

	public TitlePanePanel getThisPanel() {
		return thisPanel;
	}

	public void setThisPanel(TitlePanePanel thisPanel) {
		this.thisPanel = thisPanel;
	}

	public boolean isUseConfig() {
		return useConfig;
	}

	public void setUseConfig(boolean useConfig) {
		this.useConfig = useConfig;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitleIcon() {
		return titleIcon;
	}

	public void setTitleIcon(String titleIcon) {
		this.titleIcon = titleIcon;
	}

	public Rectangle getRectClose() {
		return rectClose;
	}

	public void setRectClose(Rectangle rectClose) {
		this.rectClose = rectClose;
	}

	public Rectangle getRectColl() {
		return rectColl;
	}

	public void setRectColl(Rectangle rectColl) {
		this.rectColl = rectColl;
	}

	public Rectangle getRectConfig() {
		return rectConfig;
	}

	public void setRectConfig(Rectangle rectConfig) {
		this.rectConfig = rectConfig;
	}

	public int getNowWidth() {
		return nowWidth;
	}

	public void setNowWidth(int nowWidth) {
		this.nowWidth = nowWidth;
	}

	public int getNowHeigth() {
		return nowHeigth;
	}

	public void setNowHeigth(int nowHeigth) {
		this.nowHeigth = nowHeigth;
	}

	public BufferedImage getBiTitle() {
		return biTitle;
	}

	public void setBiTitle(BufferedImage biTitle) {
		this.biTitle = biTitle;
	}

	public BufferedImage getBiCfg() {
		return biCfg;
	}

	public void setBiCfg(BufferedImage biCfg) {
		this.biCfg = biCfg;
	}

	public BufferedImage getBiColl() {
		return biColl;
	}

	public void setBiColl(BufferedImage biColl) {
		this.biColl = biColl;
	}

	public BufferedImage getBiExp() {
		return biExp;
	}

	public void setBiExp(BufferedImage biExp) {
		this.biExp = biExp;
	}

	public BufferedImage getBiClose() {
		return biClose;
	}

	public void setBiClose(BufferedImage biClose) {
		this.biClose = biClose;
	}

	public MouseListenerForPanelIcon getIconListener() {
		return iconListener;
	}

	public void setIconListener(MouseListenerForPanelIcon iconListener) {
		this.iconListener = iconListener;
	}

	public boolean isExpanded() {
		return expanded;
	}

	public void setExpanded(boolean expanded) {
		this.expanded = expanded;
	}
	
}
